<?php

/**
 * This is the model class for table "{{galeria}}".
 *
 * The followings are the available columns in table '{{galeria}}':
 * @property integer $id
 * @property string $name
 * @property string $author
 * @property string $link
 */
class tbl_galeria extends CActiveRecord
{


       const MAX_IMG     = 5;



	/**
	 * Returns the static model of the specified AR class.
	 * @return tbl_galeria the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cad_galeria';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name,id_author, author', 'required'),
			array('name, author', 'length', 'max'=>200),
                        // na insersao obrigatorio img
			array('link', 'file','on'=>'insert', 'types'=>'jpg, gif, png','allowEmpty'=>false),
                        // na edicao a img nao é obrigatoria
                        array('link', 'file','on'=>'update', 'types'=>'jpg, gif, png','allowEmpty'=>true),
			// Please remove those attributes that should not be searched.
			array('id, name, id_author,author', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => 'Titulo da imagem',
                        'id_author'=>'Id autor',
			'author' => 'Autor',
			'link' => 'Link',
		);
	}

	public function findImg($userid=null,$limit=20)
	{       //se houver userid(pega) senao lista todas
                $models= ($userid <> null) ? $this->findAll(array('order'=>'id', 'condition'=>'id_author=:x', 'params'=>array(':x'=>$userid))) 
                                           : $this->findAll();
		
		$imgs=array();
			foreach($models as $model)
				$imgs[] = array(Yii::app()->baseUrl.'/images/'.$model->link,'alt'=>$model->link, 'htmlOptions'=>array('title'=>$model->name));

            return $imgs;
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

                $criteria->compare('id_author',$this->id_author,true);
                
		$criteria->compare('author',$this->author,true);

		return new CActiveDataProvider('tbl_galeria', array(
			'criteria'=>$criteria,
                        'pagination'=>array(
                           'pageSize'=>3,
                          ),
		));
	}

	public function mySearch()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);

		$criteria->compare('name',$this->name,true);

                $criteria->compare('id_author',Yii::app()->user->id,true);

		$criteria->compare('author',$this->author,true);

		return new CActiveDataProvider('tbl_galeria', array(
			'criteria'=>$criteria,
                         'pagination'=>array(
                           'pageSize'=>3,
                          ),
		));
	}
}